package com.wind.taobao.api.trade.trade.convert
{
	import com.wind.taobao.api.trade.trade.model.Refund;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Refund转换器
	 * @author fxy
	 *
	 */
	public class RefundConvert
	{
		/**
		 * 转换Refund集合
		 */
		public static function convertRefunds(value:Object, type:String="json"):ArrayCollection
		{
			var refunds:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							refunds.addItem(convertRefund((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return refunds;
		}

		/**
		 * 转换单个Refund对象
		 * value:XML 或 JSON
		 */
		public static function convertRefund(value:Object, type:String="json", refund:Refund=null):Refund
		{
			if (refund == null)
				refund=new Refund();
			if (value == null)
				return refund;
			if (type == TaobaoConstants.XML)
			{
				refund=convertXMLToRefund(XML(value), refund);
			}
			else
			{
				refund=convertJSONToRefund(value, refund);
			}
			return refund;
		}

		/**
		 * JSON转换为Refund
		 */
		private static function convertJSONToRefund(json:Object, refund:Refund):Refund
		{
			if (json.hasOwnProperty(TaobaoConstants.IID))
			{
				refund.iid=json[TaobaoConstants.IID];
			}
			if (json.hasOwnProperty(TaobaoConstants.REFUND_ID))
			{
				refund.refund_id=json[TaobaoConstants.REFUND_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.TID))
			{
				refund.tid=json[TaobaoConstants.TID];
			}
			if (json.hasOwnProperty(TaobaoConstants.OID))
			{
				refund.oid=json[TaobaoConstants.OID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ALIPAYNO))
			{
				refund.alipay_no=json[TaobaoConstants.ALIPAYNO];
			}
			if (json.hasOwnProperty(TaobaoConstants.TOTAL_FEE))
			{
				refund.total_fee=json[TaobaoConstants.TOTAL_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_NICK))
			{
				refund.seller_nick=json[TaobaoConstants.SELLER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_NICK))
			{
				refund.buyer_nick=json[TaobaoConstants.BUYER_NICK];
			}
			//退款协议相关字段
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				refund.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				refund.modified=json[TaobaoConstants.MODIFIED];
			}
			if (json.hasOwnProperty(TaobaoConstants.ORDER_STATUS))
			{
				refund.order_status=json[TaobaoConstants.ORDER_STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				refund.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.GOOD_STATUS))
			{
				refund.good_status=json[TaobaoConstants.GOOD_STATUS];
			}

			if (json.hasOwnProperty(TaobaoConstants.HAS_GOOD_RETURN))
			{
				refund.has_good_return=json[TaobaoConstants.HAS_GOOD_RETURN];//已转换Boolean
			}

			if (json.hasOwnProperty(TaobaoConstants.REFUND_FEE))
			{
				refund.refund_fee=json[TaobaoConstants.REFUND_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PAYMENT))
			{
				refund.payment=json[TaobaoConstants.PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.REASON))
			{
				refund.reason=json[TaobaoConstants.REASON];
			}
			if (json.hasOwnProperty(TaobaoConstants.DESC))
			{
				refund.desc=json[TaobaoConstants.DESC];
			}
			//申请退款的商品相关字段
			if (json.hasOwnProperty(TaobaoConstants.IID))
			{
				refund.iid=json[TaobaoConstants.IID];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				refund.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PRICE))
			{
				refund.price=json[TaobaoConstants.PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.NUM))
			{
				refund.num=json[TaobaoConstants.NUM];
			}
			//退货信息相关字段
			if (json.hasOwnProperty(TaobaoConstants.GOOD_RETURN_TIME))
			{
				refund.good_return_time=json[TaobaoConstants.GOOD_RETURN_TIME];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMPANY_NAME))
			{
				refund.company_name=json[TaobaoConstants.COMPANY_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.SID))
			{
				refund.sid=json[TaobaoConstants.SID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ADDRESS))
			{
				refund.address=json[TaobaoConstants.ADDRESS];
			}
			return refund;
		}



		/**
		 * XML转换为Refund
		 */
		private static function convertXMLToRefund(value:XML, refund:Refund):Refund
		{
			return refund;
		}

	}
}